import { getToken, setToken, removeToken } from '@/utils/auth'
import { getUserProfile, getUserProfileByid } from '@/api/user'
export default {
  namespaced: true,
  state: {
    // getToken()优先从cookies中获取token
    token: getToken() || '', // 保存token
    userinfo: {}
  },
  mutations: {
    updataToken(state, token) {
      state.token = token
      // 将token存储到cookies中实现持久化
      setToken(token)
    },
    delToken(state) {
      // 清空cookies中的token
      state.token = ''
      removeToken()
    },
    updataUserinfo(state, userinfo) {
      // console.log(userinfo);
      state.userinfo = userinfo
    },
    delUserinfo(state) {
      state.userinfo = ''
    }
  },
  actions: {
    async getUserinfo(context) {
      try {
        const res = await getUserProfile()
        const info = await getUserProfileByid(res.data.userId)
        console.log(info.data, 'info')
        context.commit('updataUserinfo', { ...res.data, ...info.data })
        return res.data.roles.menus
      } catch (error) {
        console.log(error)
      }
    }
  }
}
